<?php
namespace app\common\library;


    class RedisServer{

        private static $instance;
        private static $redis;
        private static $config = [
            'host' => '127.0.0.1',
            'port' => 6379,
            'table' => 1
        ];


        private function __construct($config){
            self::$redis = new \Redis;
            self::$redis->connect(self::$config['host'],self::$config['port']);
            self::$redis->select(self::$config['table']);
        }


        public static function instance($config = []){
            if(!isset(self::$instance[self::$config['table']])){
                self::$instance[self::$config['table']] = new self($config);
            }
            return self::$instance[self::$config['table']];
        }

        private function __clone(){}

        public function get($key){
            return self::$redis->get($key);
        }

        public function set($key,$value){
            return self::$redis->set($key,$value);
        }

        public function hset($key,$hashKey,$value,$time){
            $ret = self::$redis->hset($key,$hashKey,$value);
            self::$redis->expire($key,$time*60);
            return $ret;
        }

        public function hmset($key,$value,$time){
            $ret = self::$redis->hmset($key,$value);
            self::$redis->expire($key,$time*60);
            return $ret;
        }

        public function hget($key,$hashkey){
            return self::$redis->hget($key,$hashkey);
        }

        public function hGetAll($key){
            $list =  self::$redis->hGetAll($key);
            return json_decode($list,true);
        }
    }